<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-07-28 11:39:51
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-07-28 15:00:47
 */

namespace App\WebSocketController;

use Common\Context\WebsocketContext;
use Common\Services\JwtService;
use Common\Services\MemberService;
use Common\WebSocketController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AuthController extends WebSocketController
{
    public function onClose($server, int $fd, int $reactorId): void
    {
        // TODO: Implement onClose() method.
    }

    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function onMessage($server, $frame): void
    {
        $data = json_decode($frame->data, true);

        $project_id = WebsocketContext::getContext(WebsocketContext::PROJECT_ID);

        switch ($data['type']){
            case 'platform':
                $member_id = $data['param']['member_id']??0;
                // 平台标识
                $platform_code = $data['param']['platform_code']??'';

                if (empty($member_id)) {
                    $Res =  $this->writeJson(400, [], 'param.member_id 不能为空');
                    $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                    return;
                }

                if (empty($platform_code)) {
                    $Res = $this->writeJson(400, [], 'param.platform_code 不能为空');
                    $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                    return;
                }

                $member_code = md5($member_id . $platform_code);

                $res = MemberService::getMemberByCode($member_code, $project_id);
                if (!empty($res)) {
                    $Res = $this->writeJson(200, $res, '第三方系统授权登录');
                } else {
                    $username = $project_id . '-' . time();
                    $password = '12345678';
                    $res = MemberService::register($project_id,$username, $password, $member_code);
                    $Res = $this->writeJson(200, $res['data'], '第三方系统授权登录');
                }
                $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                return;
                break;
            case 'register':
                $username = $data['param']['username']??'';
                $password = $data['param']['password']??'';
                $mobile = $data['param']['mobile']??'';
                $smsCode = $data['param']['smsCode']??'';

                if (empty($username)) {
                    $Res = $this->writeJson(400, null, 'param.username 不能为空');
                    $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                    return;
                }

                if (empty($password)) {
                    $Res = $this->writeJson(400, null, 'param.password 不能为空');
                    $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                    return;
                }

                $Res = MemberService::register($project_id,$username, $password, '', $mobile, $smsCode);
                $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                return;
                break;
            case 'login':

                $username = $data['param']['username']??"";


                if (!$username) {
                    $Res = [
                        'code' => 400,
                        'type' => 'login_res',
                        'message' => 'param.username 不能为空',
                    ];

                    $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                }
                $password = $data['param']['password']??'';
                if (!$password) {
                    $Res = [
                        'code' => 400,
                        'message' => 'param.password 不能为空',
                    ];

                    $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                }

                $Res = JwtService::createToken($username, $password, $project_id);

                $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                break;
            case 'flush-token':
                $refresh_token = $data['refresh_token']??'';
                if (empty($refresh_token)) {
                    $Res = $this->writeJson(400, null, 'refresh_token 不能为空');
                    $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                    return;
                }
                $Res = JwtService::flushToken($refresh_token, $project_id);
                $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));
                break;

            default:
                $Res = $this->writeJson(401,[],'type类型不在flush-token 、login、register、platform 中');
                $server->push($frame->fd, json_encode($Res, JSON_UNESCAPED_UNICODE));

        }

    }

    public function onOpen($server, $request): void
    {
        // TODO: Implement onOpen() method.
    }
}
